function disableOtherRows(rowId) {

    $('.category_table tr').each(function () {
        if (this.id != rowId) {
            for (var i = 0; i < this.cells.length; i++) {
                if (this.cells[i].childElementCount > 0) {
                    this.cells[i].childNodes[0].disabled = true;
                }
            }
        }
    });
    var callMap = {
        type:"POST",
        data:$('#formModify').serialize(),
        url:"categories/update",
        success:function (data) {
            $("#continut").html(data);
        },
        error:function (jqXHR, textStatus, errorThrown) {

        }
    };
    $.ajax(callMap);
}

function deleteUser(id) {

    var answer = confirm("Do you really want to delete this user?");
    if (answer) {
        $("#deleteUserId").attr("value", id);
        var callMap = {
            type:"POST",
            data:$('#deleteUser').serialize(),
            url:"users/delete",
            success:function (data) {
                $("#continut").html(data);
            }
        };
        $.ajax(callMap);
    }
}

function setInactive(id) {
    $("#questionnaireId").attr("value", id);
    var callMap = {
        type:"POST",
        data:$('#setQuestionnaireInactive').serialize(),
        url:"questionnaires/setInactive",
        success:function (data) {
            $("#continut").html(data);
        }
    };
    $.ajax(callMap);
}

function deleteCategory(id) {

    var answer = confirm("Do you really want to delete this category?");
    if (answer) {
        $("#deleteCategoryId").attr("value", id);
        var callMap = {
            type:"POST",
            data:$('#deleteCategory').serialize(),
            url:"categories/delete",
            success:function (data) {
                $("#continut").html(data);
            },
            error:function (jqXHR, textStatus, errorThrown) {

            }
        };
        $.ajax(callMap);
    }
}

function showRows() {
    var selected = parseInt($("#noOfResponses :selected").text());
    var divResponse = $('#responseContainer');
    var index;
    if (divResponse.is(':empty')) {
        index = 0;
    }
    else {
        index = $('#responseContainer textarea').length;
    }
    if (selected == index) {
        return;
    }
    var textareaHTML = "";
    if (selected > index) {
        //add extra rows(selected-index)
        var oldContent = divResponse.html();
        for (var i = index; i < selected; i++) {
            textareaHTML = textareaHTML + ("<textarea name='responses[" + i + "].text' class=''/><br/>");
        }
        divResponse.empty();
        divResponse.html(oldContent + textareaHTML);
    }
    else {
        for (var i = 0; i < selected; i++) {
            var oldTextAreaValue = $('#responseContainer textarea:eq(' + i + ')').val();
            textareaHTML = textareaHTML
                + ("<textarea name='responses[" + i + "].text' class=''>" + oldTextAreaValue + "</textarea><br/>")
        }
        divResponse.empty();
        divResponse.html(textareaHTML);
    }
}

function showNewQuestionForm() {
    var callMap = {
        type:"GET",
        data:"",
        url:"addQuestion",
        success:function (data) {
            $("#newQuestion").html(data);
        }

    };
    $.ajax(callMap);
}

function submitQuestion() {
    var callMap = {
        type:"POST",
        data:$("#addQuestionForm").serialize(),
        url:"questions/add",
        success:function (data) {

            if (data.textContent != null) {
                $("#newQuestion").html(data);
            }
            else {
                $("#newQuestion").empty();
                getSavedQuestions();
            }
        },

        error:function (jqXHR, textStatus, errorThrown) {
            $("#newQuestion").empty();
            getSavedQuestions();
        }
    };
    $.ajax(callMap);
}

function getSavedQuestions() {

    var callMap = {
        type:"GET",
        data:"",
        url:"addQuestionnaire/savedQuestions",
        success:function (data) {

            $("#questionsSaved").html(data);
        }
    };
    $.ajax(callMap);
}


var ok = true;
function showQuestionnaireTitles(categoryId) {
    var divId = 'div_' + categoryId;
    var callMap = {
        type:"GET",
        data:'',
        url:'getQFromCategory?category_id=' + categoryId,
        success:function (data) {
            if (ok == true) {
                $("#" + divId).html(data);
                ok = false;
            } else {
                $("#" + divId).html("");
                ok = true;
            }
        }
    };
    $.ajax(callMap);
}

function viewQuestionnaireContent(questionnaireId) {
    // var divId = 'questionnaire_content';
    var callMap = {
        type:"GET",
        data:'',
        url:'getQContent?q_id=' + questionnaireId,
        success:function (data) {
            $("#continut").html(data);
            $("#successSubmit").hide();
        }
    };
    $.ajax(callMap);
}

function submitQuestionnaire() {
    var divId = 'successSubmit';
    $('#q_content :checkbox').each(function() {
        if (!$(this).attr('checked')) {
            $(this).attr('checked', true);
            $(this).attr('value', 0);
        }
    });
    var callMap = {
        type:'POST',
        data:$("#q_content").serialize(),
        url:'submitQuestionnaire',
        success:function (data) {
            //$("#" + divId).show();
        },
        error:function (jqXHR, textStatus, errorThrown) {
            $("#submitQ").hide();
            $('#q_content :checkbox').each(function() {
                if ($(this).attr('checked') && $(this).attr('value') == '0') {
                    $(this).attr('checked', false);
                }
            });
            $("#" + divId).show();
        }
    };
    $.ajax(callMap);
}

function showStatistics(questionnaireId) {
    var callMap = {
        type:"GET",
        data:"",
        url:"showStatistics?questionnaireId=" + questionnaireId,
        success:function (data) {
            $("#continut").html(data);
        }
    };
    $.ajax(callMap);
}

function viewUsers() {
    var callMap = {
        type:"GET",
        data:"",
        url:"users",
        success:function (data) {
            $("#continut").html(data);
        }
    };
    $.ajax(callMap);
}

function manageCategories() {
    var callMap = {
        type:"GET",
        data:"",
        url:"categories",
        success:function (data) {
            $("#continut").html(data);
        }
    };
    $.ajax(callMap);
}

function manageQuestionnaires() {
    var callMap = {
        type:"GET",
        data:"",
        url:"questionnaires",
        success:function (data) {
            $("#continut").html(data);
        }
    };
    $.ajax(callMap);
}

function addUser() {
    var callMap = {
        type:"POST",
        data:$("#add_user_form").serialize(),
        url:"addUser",
        success:function (data) {
            $("#continut").html(data);
        }
    };
    $.ajax(callMap);
}

function addQuestionnaire() {
    var callMap = {
        type:"GET",
        data:"",
        url:"addQuestionnaire",
        success:function (data) {
            $("#continut").html(data);
        }
//        error:function (jqXHR, textStatus, errorThrown) {
//            alert('ERROR');
//        }
    };
    $.ajax(callMap);
}

function addCategory() {
    var callMap = {
        type:"POST",
        data:$("#addCatForm").serialize(),
        url:"add",
        success:function (data) {
            $("#continut").html(data);
        },
        error:function (jqXHR, textStatus, errorThrown) {
            alert('ERROR');
            var a=10;
        }
    };
    $.ajax(callMap);

}

function addQ() {
    var callMap = {
        type:"POST",
        data:$('#createQ').serialize(),
        url:"questionnaires/add",
        success:function (data) {
            $("#newQuestion").html(data);
        },
        error:function (jqXHR, textStatus, errorThrown) {

        }
    };
    $.ajax(callMap);
}

function logout() {
    var answer = confirm("Do you really want to logout?");
    if (answer) {
        $("#logout_form").submit();
    }
}

function gotToHomePage() {
    window.location = " index";
}
